
package com.yuan.unit;

import com.yuan.graduate.BuildConfig;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;

/**
 * 系统信息
 * 
 * @author YUAN
 */
public class SystemInfo {
    public static boolean DEBUG = BuildConfig.DEBUG;
    public static final String mBuildEnvNumber = "000";
    public static int versionCode;
    public static String versionName;

    public static final String DEFAULT_IMEI = "GRADUATE_DEFAULT_IMEI";
    public static final String DEFAULT_MAC = "GRADUATE_DEFAULT_MAC";
    private final static String BOOT_DEVICE_ID = "boot_device_id";

    public static String getBuildNumber() {
        if (DEBUG) {
            return " build" + mBuildEnvNumber;
        } else {
            return "";
        }
    }

    /**
     * Use this in user usage report
     */
    public static String deviceId;
    public static float density;
    public static int densityDpi;
    public static int widthPixels;
    public static int heightPixels;
    public static int statebarHeight;

    public static void init(final Context context, final SharedPreferences pref,
            final Editor editor, final Resources resource) {
        updateDimension(resource);
        // version info
        try {
            PackageInfo pi = context.getPackageManager().getPackageInfo(
                    context.getPackageName(),
                    PackageManager.GET_CONFIGURATIONS);
            versionCode = pi.versionCode;
            versionName = pi.versionName;
        } catch (Exception e) {
            e.printStackTrace();
        }
        deviceId = pref.getString(BOOT_DEVICE_ID, null);
        if (deviceId == null) {
            deviceId = StringUtils.MD5Encode(getImei(context) + getWifiMac(context));
            editor.putString(BOOT_DEVICE_ID, deviceId);
        }
        editor.commit();
        editor.commit();
    }

    public final static void updateDimension(Resources resource) {
        final DisplayMetrics displayMetrics = resource.getDisplayMetrics();
        density = displayMetrics.density;
        densityDpi = displayMetrics.densityDpi;
        widthPixels = displayMetrics.widthPixels;
        heightPixels = displayMetrics.heightPixels;
    }

    public final static String getImei(Context ctx) {
        TelephonyManager tm = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE);
        try {
            if (tm != null && tm.getDeviceId() != null) {
                return tm.getDeviceId();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return DEFAULT_IMEI;
    }

    public final static String getWifiMac(Context ctx) {
        try {
            WifiManager wifi = (WifiManager) ctx.getSystemService(Context.WIFI_SERVICE);
            WifiInfo info = wifi.getConnectionInfo();
            if (info != null && info.getMacAddress() != null) {
                return info.getMacAddress();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return DEFAULT_MAC;
    }

}
